package com.yx.cloud.activity;

import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.iot.product.sweeper.config.SweeperProductConfig;
import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.constant.Constants;
import com.yx.cloud.databinding.RobotarchitectureActicityBinding;
import com.yx.cloud.product.ProductConfigConstants;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.LanguageUtils;
import com.yx.cloud.utils.RomUtils;

import org.json.JSONObject;

/**
 * 机器人结构图界面
 */
public class RobotArchitectureActicity extends BaseActivity implements View.OnClickListener {

    private String tag;
    private String language;
    private String strProductType;
    String Chinese_url = "", Other_url = "";

    private RobotarchitectureActicityBinding binding;

    @Override
    public void onCreate() {
        binding = RobotarchitectureActicityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initData();
    }

    private void initData() {

        JSONObject productConfig = AppContext.getConfig();

        strProductType = productConfig.optString(ProductConfigConstants.productGuide);

        tag = getIntent().getStringExtra("tag");
        //获取设置的语言 默认是中文
        language = getIntent().getStringExtra("language");

        //根据语言后缀组装url
        String systemLanguage = LanguageUtils.getProductSuffix(this).toUpperCase();
        if (language.equals(Constants.TW)) {
            systemLanguage = "TW";
        }

        binding.webview.setVerticalScrollBarEnabled(false); //不显示垂直滚动条

        binding.webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });
        if (tag.equals("1")) {
            //机器人结构图
            binding.tvTitle.setText(getString(R.string.U_RobotStructure));
            Chinese_url = "http://info.inxni.com/ProductGuide/" + strProductType + "/RobotStructure.html";
            Other_url = "http://info.inxni.com/ProductGuide/" + strProductType + "/RobotStructure_" + systemLanguage + ".html";
        } else if (tag.equals("2")) {
            //机器人使用方法
            binding.tvTitle.setText(getString(R.string.U_UsageMethod));
            Chinese_url = "http://info.inxni.com/ProductGuide/" + strProductType + "/UsageMethod.html";
            Other_url = "http://info.inxni.com/ProductGuide/" + strProductType + "/UsageMethod_" + systemLanguage + ".html";
        } else if (tag.equals("3")) {
            //日常维护
            binding.tvTitle.setText(getString(R.string.U_DailyMaintenance));
            Chinese_url = "http://info.inxni.com/ProductGuide/" + strProductType + "/RoutineMaintenance.html";
            Other_url = "http://info.inxni.com/ProductGuide/" + strProductType + "/RoutineMaintenance_" + systemLanguage + ".html";
        } else if (tag.equals("4")) {
            //故障排除
            binding.tvTitle.setText(getString(R.string.U_Troubleshooting));
            Chinese_url = "http://info.inxni.com/ProductGuide/" + strProductType + "/Troubleshooting.html";
            Other_url = "http://info.inxni.com/ProductGuide/" + strProductType + "/Troubleshooting_" + systemLanguage + ".html";
        } else if (tag.equals("5")) {
            //第三方语音助手
            binding.tvTitle.setText(getString(R.string.U_VoiceAssistant));
            if (productConfig.optBoolean(ProductConfigConstants.isInxni)) { //第三方语音助手 除了以内的产品 其他都用lf21的网页
                if (systemLanguage.equals(Constants.TW)) {
                    Chinese_url = "http://info.inxni.com/ProductGuide/X3/ThirdPartyVoice_EN.html";
                } else {
                    Chinese_url = "http://info.inxni.com/ProductGuide/X3/ThirdPartyVoice.html";
                    Other_url = "http://info.inxni.com/ProductGuide/X3/ThirdPartyVoice_EN.html";
                }
            } else {
                Chinese_url = "http://info.inxni.com/ProductGuide/LF21/ThirdPartyVoice.html";
                Other_url = "http://info.inxni.com/ProductGuide/LF21/ThirdPartyVoice_EN.html";
            }
        }
        binding.webview.setWebViewClient(new MyWebViewClient());
        binding.webview.loadUrl("简体中文".equals(language) ? Chinese_url : Other_url);
        binding.ivReturn.setOnClickListener(this);

    }

    public class MyWebViewClient extends WebViewClient {
        @Override
        public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
            super.onReceivedHttpError(view, request, errorResponse);
            if (errorResponse.getStatusCode() == 404) {
                binding.webview.loadUrl(Chinese_url);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        int id = v.getId();
        if (id == R.id.iv_return) {
            Intent_utils.backIntent(this);
        }
    }
}
